import 'package:base_lib/common/base/base_bloc_page.dart';
import 'package:base_lib/index.dart';
import 'package:base_lib/router/parameters.dart';
import 'package:base_lib/theme/theme_global.dart';
import 'package:doctor_login_lib/disease/bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';

///专治疾病
class DiseasePage extends BaseBlocPage<DiseaseBloc, _DiseasePageState> {
  DiseasePage(Parameters parameters) : super(parameters);

  @override
  _DiseasePageState generateThemeState() {
    return _DiseasePageState();
  }
}

class _DiseasePageState extends BaseBlocPageState<DiseaseBloc>
    with AfterLayoutMixin {
  TextEditingController _editingController = TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  @override
  void afterFirstLayout(BuildContext context) {
    bloc.getDoctorDisease(parameters.getString("keyID"));
  }

  @override
  void dispose() {
    _editingController.dispose();
    super.dispose();
  }

  @override
  Widget buildChild(BuildContext context, MTheme theme) {
    return Scaffold(
      appBar: AppBar(
        leading: BackButton(
          onPressed: () {
            Routers.pop(context);
          },
        ),
        centerTitle: true,
        title: Text("专治疾病"),
        actions: [
          FlatButton(
            onPressed: () {
              bloc.save(context, parameters.getString("keyID"));
            },
            child: Text(
              "确定",
              style: TextStyle(color: Colors.white),
            ),
          )
        ],
      ),
      body: StreamBuilder(
        stream: bloc.loading,
        initialData: true,
        builder: (context, AsyncSnapshot<bool> loading) {
          return loading.data
              ? buildLoadingIndicator()
              : Container(
                  child: Column(
                    children: [
                      Container(
                        alignment: Alignment.center,
                        width: double.infinity,
                        color: ColorUtil.color("ffeeeb"),
                        padding: EdgeInsets.all(10),
                        child: Text(
                          "*请慎重选择专治疾病，系统将根据标签排序为您推荐患者",
                          style: TextStyle(
                              color: ColorUtil.color("ff6849"),
                              fontSize: theme.themeFontSize.fontSizeLittle),
                        ),
                      ),
                      (bloc.chipList.length <= 1)
                          ? Container(
                              alignment: Alignment.center,
                              padding: EdgeInsets.symmetric(
                                  horizontal: 15, vertical: 30),
                              child: Text(
                                "立即添加专治疾病标签～",
                                style: TextStyle(color: Colors.grey),
                              ),
                            )
                          : SizedBox(),
                      Expanded(
                        child: Container(
                          padding: EdgeInsets.all(10),
                          child: Container(
                            width: double.infinity,
                            child: Wrap(
                              spacing: 10,
                              runSpacing: 5,
                              alignment: WrapAlignment.start,
                              direction: Axis.horizontal,
                              children: bloc.chipList
                                  .map((e) => _buildChip(e, theme))
                                  .toList(),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                );
        },
      ),
    );
  }

  @override
  DiseaseBloc generateBloc() {
    return DiseaseBloc();
  }

  Widget _buildChip(String e, MTheme theme) {
    return (e == "+")
        ? ActionChip(
            onPressed: () {
              _editingController.clear();
              showDialog<Null>(
                context: context,
                barrierDismissible: true,
                builder: (BuildContext context) {
                  return new AlertDialog(
                    title: new Text('专治疾病'),
                    content: new SingleChildScrollView(
                      child: new ListBody(
                        children: <Widget>[
                          TextField(
                            controller: _editingController,
                            decoration: InputDecoration(hintText: "请输入专治疾病..."),
                          )
                        ],
                      ),
                    ),
                    actions: <Widget>[
                      new FlatButton(
                        child: new Text('取消'),
                        onPressed: () {
                          Navigator.pop(context);
                        },
                      ),
                      new FlatButton(
                        child: new Text('确定'),
                        onPressed: () {
                          if (ObjectUtil.isNotEmpty(_editingController.text)) {
                            Navigator.pop(context);
                            setState(() {
                              bloc.add(_editingController.text);
                            });
                          } else {
                            ToastUtil.warning("请先填写专治疾病");
                          }
                        },
                      ),
                    ],
                  );
                },
              ).then((val) {
                print(val);
              });
              //   CustomDialog.customDialog(context,
              //       child: Container(
              //         color: Colors.white,
              //         height: 60,
              //         child: Row(
              //           children: [
              //             10.hGapDp,
              //             Expanded(
              //               child: TextField(
              //                 controller: _editingController,
              //               ),
              //             ),
              //             5.hGapDp,
              //             RaisedButton(
              //               color: theme.themeColor.themeColor.withAlpha(66),
              //               textColor: Colors.white,
              //               child: Text("确定"),
              //               onPressed: () {
              // var string = _editingController.text;
              // Navigator.pop(context);
              // setState(() {
              //   bloc.add(string);
              // });
              //               },
              //             ),
              //             10.hGapDp,
              //           ],
              //         ),
              //       ));
            },
            label:
                Text(e, style: TextStyle(color: theme.themeColor.themeColor)),
            padding: EdgeInsets.all(5),
            backgroundColor: ColorUtil.color("#EAF5FF"),
            shadowColor: theme.themeColor.themeColor,
            elevation: 2,
            pressElevation: 4,
          )
        : Chip(
            label: Text(
              e,
              style: TextStyle(color: theme.themeColor.themeColor),
            ),
            padding: EdgeInsets.all(5),
            backgroundColor: ColorUtil.color("#EAF5FF"),
            shadowColor: theme.themeColor.themeColor,
            elevation: 2,
            deleteIconColor: Colors.red,
            onDeleted: () {
              setState(() {
                bloc.doDelete(e);
              });
            },
          );
  }
}
